package kz.kstu.scantest.business.client.excel;

import kz.kstu.scantest.business.common.helper.FormDataHelper;
import kz.kstu.scantest.business.entity.data.FormData;
import kz.kstu.scantest.business.entity.data.SubjectData;
import kz.kstu.scantest.business.entity.data.SubjectEnum;
import kz.kstu.scantest.business.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;

/**
 * @author Filipp_Stankevich
 *         created: 10.10.11 23:41
 */
public class TestResultExcelExporter {

    private final static String[] COLUMN_NAMES = new String []{"№", "Фамилия И.О.", "Литер", "Вариант", "Предмет1",
            "Балл1", "Предмет2", "Балл2", "Предмет3", "Балл3", "Предмет4", "Балл4", "Предмет5", "Балл5","Сумма"};

    public void export(String  fileName, Collection<FormData> formDataCollection) throws IOException {
        FileOutputStream outputStream = new FileOutputStream(fileName);
        Workbook workbook = new HSSFWorkbook();
        fillWorkBook(workbook, formDataCollection);
        workbook.write(outputStream);
        outputStream.close();
    }

    private void fillWorkBook(Workbook workbook, Collection<FormData> formDataCollection) {

        Sheet sheet = workbook.createSheet();

        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());

        int rowNumber = 1;

        Row title = sheet.createRow(rowNumber);
        for (int i = 0; i < COLUMN_NAMES.length; i++) {
            Cell cell = title.createCell(i + 1);
            cell.setCellValue(COLUMN_NAMES[i]);
            cell.setCellStyle(style);
        }

        for (FormData formData : formDataCollection) {
            int columnIndex = 1;
            int rowIndex = rowNumber + 1;
            Row record  = sheet.createRow(rowIndex);
            Cell numberCell =  record.createCell(columnIndex++);
            numberCell.setCellStyle(style);
            numberCell.setCellValue(rowNumber);

            Cell nameCell =  record.createCell(columnIndex++);
            String name = formData.getSurname() + StringUtil.SPACE + formData.getInitials();
            nameCell.setCellValue(name);
            nameCell.setCellStyle(style);

            Cell literCell =  record.createCell(columnIndex++);
            literCell.setCellValue(formData.getClassLetter());
            literCell.setCellStyle(style);

            Cell variantCell =  record.createCell(columnIndex++);
            variantCell.setCellValue(formData.getVariant());
            variantCell.setCellStyle(style);

            int subOfBall = 0;
            for (SubjectData subjectData : formData.getSubjects()) {
                Cell subjectNameCell =  record.createCell(columnIndex++);
                SubjectEnum subjectEnum = subjectData.getSubjectEnum();
                String subjectName =  subjectEnum != null ? subjectEnum.getSubjectName() : StringUtil.EMPTY_STRING;
                subjectNameCell.setCellValue(subjectName);
                subjectNameCell.setCellStyle(style);

                Cell subjectBallCell =  record.createCell(columnIndex++);
                int subjectBall = FormDataHelper.getSubjectBall(subjectData);
                subjectBallCell.setCellValue(subjectBall);
                subjectBallCell.setCellStyle(style);

                subOfBall += subjectBall;
            }

            Cell subjectBallCell =  record.createCell(columnIndex);
            subjectBallCell.setCellValue(subOfBall);
            subjectBallCell.setCellStyle(style);

            rowNumber++;
        }

        for (int i = 0; i < COLUMN_NAMES.length; i++) {
            sheet.autoSizeColumn(i + 1);
        }
    }
}
